-- Copyright 2008 Steven Barth <steven@midlink.org>
-- Copyright 2010-2012 Jo-Philipp Wich <jow@openwrt.org>
-- Licensed to the public under the Apache License 2.0.

local ds = require "luci.dispatcher"
local ft = require "luci.tools.firewall"

m = Map("firewall",                                                                                                                                             
        translate("Firewall - SNAT Rules"),                                                                                                                     
        translate("This page can change or create a SNAT rule that matches the source address of the rule traffic to be rewritten to specify the IP address. "))

local function rule_src_txt(self, s)
	local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone"))
	local a = ft.fmt_ip(self.map:get(s, "src_ip"), translate("any host"))
	local p = ft.fmt_port(self.map:get(s, "src_port"))
	local m = ft.fmt_mac(self.map:get(s, "src_mac"))

	if p and m then
		return translatef(" %s<br>%s(%s)", a, p, m)
	elseif p or m then
		return translatef(" %s<br>%s", a, p or m)
	else
		return translatef(" %s  ", a)
	end
end

local function snat_dest_txt(self, s)
	local z = ft.fmt_zone(self.map:get(s, "dest"), translate("any zone"))
	local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
	local p = ft.fmt_port(self.map:get(s, "dest_port")) or
		ft.fmt_port(self.map:get(s, "src_dport"))

	if p then
		return translatef("%s<br>%s", a, p)
	else
		return translatef("%s", a)
	end
end


--
-- SNAT
--

s = m:section(TypedSection, "redirect",
	translate("SNAT Rule"),
	translate("Source NAT is a specific form of masquerading which allows \
		fine grained control over the source IP used for outgoing traffic, \
		for example to map multiple WAN addresses to internal subnets."))
s.template  = "cbi/tblsection"
s.addremove = true
s.anonymous = true
s.sortable  = false
s.extedit   = ds.build_url("admin/network/firewall/snat/%s")
s.extadd = ds.build_url("admin/network/firewall/snat/new")
-- s.template_addremove = "firewall/cbi_addsnat"

-- 只返回SNAT的规则
function s.filter(self, sid)
	return (self.map:get(sid, "target") == "SNAT")
end

ft.opt_name(s, DummyValue, translate("Rule name")).width = "10%"

--方向
dir = s:option(DummyValue, "dir", translate("Direction"))
dir.rawhtml = true
dir.width   = "10%"
function dir.cfgvalue(self, s)
	
	local za = self.map:get(s, "src")
	local zd = self.map:get(s, "dest")
	return translatef("<var>%s</var> -> <var>%s</var>", za, zd)

end

local function forward_proto_txt(self, s)
	return "%s-%s" %{
		translate("IPv4"),self.map:get(s, "proto") or "TCP+UDP"
	}
end

--协议
proto = s:option(DummyValue, "proto", translate("Protocol"))
proto.rawhtml = true
proto.width   = "10%"
function proto.cfgvalue(self, s)
	return "<small>%s</small>" % {
		forward_proto_txt(self, s)
	}
end

--匹配源流量
src_flow = s:option(DummyValue, "src_flow", translate("From"))
src_flow.rawhtml = true
src_flow.width   = "15%"
function src_flow.cfgvalue(self, s)
	return "<small>%s</small>" % {
		rule_src_txt(self, s)
	}
end

--访问目的
proto = s:option(DummyValue, "dst_flow", translate("To"))
proto.rawhtml = true
proto.width   = "15%"
function proto.cfgvalue(self, s)
	return "<small>%s</small>" % {
		snat_dest_txt(self, s)
	}
end

--改写成
snat = s:option(DummyValue, "via", translate("Translate"))
snat.rawhtml = true
snat.width   = "15%"
function snat.cfgvalue(self, s)
	local a = ft.fmt_ip(self.map:get(s, "src_dip"))
	local p = ft.fmt_port(self.map:get(s, "src_dport"))

	if a and p then
		return translatef("Rewrite to source %s, %s", a, p)
	else
		return translatef("Rewrite to source %s", a or p)
	end
end

ft.opt_enabled(s, Flag, translate("Enable")).width = "10%"


return m
